package dao.impl;

import bean.UserInfo;
import dao.IUserInfoDao;
import utils.Conn;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserInfoDaoImpl implements IUserInfoDao {
    Conn conn = new Conn();
    @Override
    public List<UserInfo> queryUserInfo() throws Exception{
        List<UserInfo> list=new ArrayList<UserInfo>();

        String sql="select * from teacher";

        ResultSet rs = conn.query(sql);

        while (rs.next()){

            int id = rs.getInt("id");

            String name = rs.getString("name");

            int phonrnumber = rs.getInt("phonenumber");

            UserInfo userInfo=new UserInfo();

            userInfo.setId(id);
            userInfo.setName(name);
            userInfo.setPhoneNumber(phonrnumber);

            list.add(userInfo);


        }
        return list;
    }

    @Override
    public boolean insertUserInfo(UserInfo userInfo) throws  Exception{
        int id = userInfo.getId();

        String name = userInfo.getName();

        int phoneNumber = userInfo.getPhoneNumber();

        String sql="insert into teacher values("+id+",'"+name+"',"+phoneNumber+")";

        return conn.insert(sql);
    }

    @Override
    public boolean updateUserInfo(UserInfo userInfo) throws  Exception{
        int id = userInfo.getId();
        String name = userInfo.getName();

        String sql="update teacher set name='"+name+"' where id="+id;

        return conn.update(sql);
    }

    @Override
    public boolean deleteUserInfo(int id) throws Exception{
        String sql="delete from teacher where id="+id;

        return conn.del(sql);
    }
}
